package ext.tianma.bom.eco;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTCollection;
import wt.log4j.LogR;
import wt.part.LineNumber;
import wt.part.Quantity;
import wt.part.QuantityUnit;
import wt.part.SubstituteQuantity;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.part.WTPartSubstituteLink;
import wt.part.WTPartUsageLink;
import wt.util.WTPropertyVetoException;
import wt.vc.VersionControlHelper;
import wt.vc.wip.WorkInProgressHelper;
import ext.com.core.CoreUtil;
import ext.tianma.bom.bean.BOMBean;
import ext.tianma.bom.util.GenericUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.model.TMECOITEMModel;
import ext.tianma.number.util.PartUtil;
import ext.tianma.part.MyPartUtil;

public class BOMECOUpdate {
	private static final Logger log = LogR.getLogger(BOMECOUpdate.class.getName());

	public static String Commit = "Commit";
	public static String Submit = "Submit";

	/**
	 * 新增物料
	 * 
	 */
	public static String addToBom(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 料号
				String partNumber = job.getString("newaddnum");
				// 用量
				String amount = job.getString("amount");
				// 视图
				String view = job.getString("view");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				
				bean.setEcoid(oid);
				bean.setType(BOMBean.ADDPart);
				bean.setEffectnumber(effectNumber);
				bean.setView(view);
				bean.setOldpartnumber(partNumber);
				bean.setOldpartamount(amount);
				System.out.println(
						"==============addToBom:" + partNumber + "," + amount + "," + view + "," + effectNumber + ";");
				// 新增物料
				TMECOITEMModel itemmodel = ECOCRUDUtil.createECO(bean);
				if (itemmodel != null) {
					result = "添加成功";
				}
			}
		} catch (Exception e) {
			result += "新增失败：" + e.getMessage();
			log.error(e.getMessage(), e);
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();

	}

	/**
	 * 创建物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 * @throws Exception
	 */
	public static String addPartToBom(BOMBean bean, String type) {
		System.out.println("=======addPartToBom========" + type);
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======addPartToBom bompart========" + ver);
			/*
			 * for (WTPart part1 : list) { if
			 * (part1.getViewName().equals(bean.getView())) { bompart = part1;
			 * System.out.println("=======addPartToBom========"+type); } }
			 */
			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			part = GenericUtil.getPart(bean.getOldpartnumber(), "", bean.getView());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getOldpartnumber());
			if(part!=null){
				childmaster = (WTPartMaster)part.getMaster();
			}
			if (bompart != null && part != null) {
				// 检出受影响对象
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				List<String> partlist = new ArrayList<String>();
				//存放行号  避免填写的行号已存在于关系中
				List<String> linenumberlist = new ArrayList<String>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					HashMap hm = new HashMap<String, String>();
					// A 父节点 B 子节点 ((WTPartMaster)
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber().toString();
					if (puse.getLineNumber() != null) {
						System.out.println("=============linklinenumber:" + puse.getLineNumber().toString());
					}
					linenumberlist.add(linenumber);
					partlist.add(number);
				}
				System.out.println("=======addPartToBom partlist========" + partlist);

				// 创建
				// if (!partlist.contains(bean.getOldpartnumber())) {
				WTPartUsageLink newuserlink = WTPartUsageLink.newWTPartUsageLink(bompart, childmaster);
				String amout1 = bean.getOldpartamount();
				Quantity qu = Quantity.newQuantity();
				qu.setUnit(part.getDefaultUnit());
				if (!BOMHelp.isNullOrEmpty(amout1)) {
					System.out.println("=======number:" + part.getNumber() + "=====UNIT======" + part.getDefaultUnit());
					qu.setAmount(Double.parseDouble(amout1));
				}
				// qu.setUnit(QuantityUnit.EA);
				newuserlink.setQuantity(qu);
				int linenum = BOMHelp.getMaxLineNumber(bompart);
				System.out.println("===========linenumber:" + linenum);
				System.out.println("===========getOldpartlinenumber:" + bean.getOldpartlinenumber());
				// 行号为空时自动设置行号

				try {
					if (BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
						newuserlink.setLineNumber(LineNumber.newLineNumber(linenum + 10));
					} else {
						if(linenumberlist.contains(bean.getOldpartlinenumber())){
							newuserlink.setLineNumber(LineNumber.newLineNumber(linenum + 10));
						}else{
							newuserlink.setLineNumber(LineNumber.newLineNumber(Integer.parseInt(bean.getOldpartlinenumber())));
						}
					}

				} catch (WTPropertyVetoException e) {
					result += e.getMessage()+";";
					System.out.println("=======addPartToBom WTPropertyVetoException========" + e);
					log.error(e.getMessage(), e);
				}
				if (Commit.equals(type)) {
					System.out.println("=======addPartToBom Commit========");
					PersistenceHelper.manager.save(newuserlink);
				} else if (Submit.equals(type)) {
					ECOCRUDUtil.createECO(bean);
				}
				// 检入受影响对象
				GenericUtil.checkin(bompart);
			}

			System.out.println("=======addPartToBom checkin========");
			// }
		} catch (Exception e) {
			System.out.println("=======addPartToBom Exception========" + e);
			result += e.getMessage()+";";
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 删除物料
	 */
	public static String deletaPartBom(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				// 物料号
				String partNumber = job.getString("partnumber");
				// 用量
				String amount = job.getString("amount");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 行号
				String linenumber = job.getString("linenumber");
				String view = job.getString("view");
				BOMBean bean = new BOMBean();
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				
				bean.setEcoid(oid);
				bean.setType(BOMBean.DelPart);
				bean.setOldpartnumber(partNumber);
				bean.setEffectnumber(effectNumber);
				bean.setView(view);
				bean.setOldpartlinenumber(linenumber);
				bean.setOldpartamount(amount);
				// 创建
				ECOCRUDUtil.createECO(bean);
				// delete
				// deletePartBom(bean,Submit);

			}
		} catch (JSONException e) {
			log.error(e.getMessage(), e);
			result = e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 删除物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 */
	public static String deletePartBom(BOMBean bean, String type) {
		System.out.println("=======deletePartBom start========");
		String result = "";
		// 先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			System.out.println("=======deletePartBom bompart number========" + bomnumber + "===view:" + bean.getView());
			if (!BOMHelp.isNullOrEmpty(bean.getView())) {
				bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			}
			System.out.println("=======deletePartBom bompart========" + bompart);
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======deletePartBom bompart========" + ver);
			/*
			 * for (WTPart part1 : list) { if
			 * (part1.getViewName().equals(bean.getView())) { bompart = part1; }
			 * }
			 */

			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			if (bompart != null && part != null) {
				// 检出
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				Map<String, WTPartUsageLink> linenumberlist = new HashMap<String, WTPartUsageLink>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber() == null ? "" : puse.getLineNumber().toString();
					linklist.put(number, puse);
					System.out.println("========deletePartBom link linenumber==========:" + linenumber);
					linenumberlist.put(linenumber, puse);
				}
				System.out.println("========linklist.size()==========" + linklist.size());
				// 找出并删掉关系
				if (linklist.containsKey(bean.getOldpartnumber())) {
					if (Commit.equals(type)) {
						System.out
								.println("========deletePartBom oldlinenumber==========" + bean.getOldpartlinenumber());
						System.out.println(
								"========deletePartBom==========" + linenumberlist.get(bean.getOldpartlinenumber()));
						if (bean.getOldpartlinenumber() != null
								&& linenumberlist.containsKey(bean.getOldpartlinenumber())) {
							System.out.println("========deletePartBom by  linenumberlist==========");
							PersistenceHelper.manager.delete(linenumberlist.get(bean.getOldpartlinenumber()));
						} else {
							System.out.println("========deletePartBom by linklist==========");
							PersistenceHelper.manager.delete(linklist.get(bean.getOldpartnumber()));
						}
					}
					/*
					 * if (Submit.equals(type)) { ECOCRUDUtil.createECO(bean); }
					 * else if (Commit.equals(type)) { System.out.println(
					 * "========delete link==========");
					 * PersistenceHelper.manager.delete(linklist.get(bean
					 * .getOldpartnumber())); }
					 */
				}
				// 检入
				if (WorkInProgressHelper.isCheckedOut(bompart)) {
					GenericUtil.checkin(bompart);
				}
				System.out.println("========deletePartBom  end==========");
			}
		} catch (Exception e) {
			result +=e.getMessage()+";";
			System.out.println("================删除关系失败===========e:" + e);
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 替换物料
	 */
	public static String replacePartBom(JSONArray array) {
		JSONObject object = new JSONObject();
		String result = "";
		try {
			for (int i = 0; i < array.length(); i++) {

				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 原料号
				String oldpartNumber = job.getString("oldnumber");
				// 原料号用量
				String oldamount = job.getString("oldamount");
				// 原料号行号
				String oldlinenumber = job.getString("oldlinenumber");
				// 新料号
				String newpartNumber = job.getString("newnumber");
				// 新料号用量
				String newpartamount = job.getString("newamount");
				// 新料号行号
				if (job.has("newlinenumber")) {
					String newpartlinenumber = job.getString("newlinenumber");
					bean.setNewpartlinenumber(newpartlinenumber);
				}
				// String newpartlinenumber = job.getString("newlinenumber");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setType(BOMBean.ReplacePart);
				bean.setOldpartnumber(oldpartNumber);
				bean.setOldpartamount(oldamount);
				bean.setOldpartlinenumber(oldlinenumber);
				bean.setNewpartnumber(newpartNumber);
				bean.setNewpartamount(newpartamount);
				// bean.setNewpartlinenumber(newpartlinenumber);
				bean.setEffectnumber(effectNumber);
				bean.setView(view);

				// 先删除原料
				// deletePartBom(bean,Submit);
				// bean.setOldpartnumber(newpartNumber);
				// 新增新料
				// addPartToBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);
			}
		} catch (Exception e) {
			result += "替换物料失败：" + e.getMessage();
			log.error(e.getMessage(), e);
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}

		return object.toString();
	}

	/**
	 * 用量需改
	 */
	public static String updatePartBom(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {

				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 原料号
				String oldpartNumber = job.getString("oldnumber");
				// 变更前用量
				String oldamount = job.getString("oldamount");
				// 行号
				String oldlinenumber = job.getString("oldlinenumber");
				// 变更后用量
				String newpartamount = job.getString("newamount");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.UpdatePart);
				bean.setOldpartnumber(oldpartNumber);
				bean.setOldpartamount(oldamount);
				bean.setOldpartlinenumber(oldlinenumber);
				bean.setNewpartamount(newpartamount);
				bean.setEffectnumber(effectNumber);

				// 更新用量
				// updateBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);
			}
		} catch (JSONException e) {
			result = e.getMessage();
			log.error(e.getMessage(), e);
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}
	
	/**
	 * 替代料用量修改（增加数据）
	 * @param array
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月25日
	 */
	public static String addSubstitutesQuantity(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 替代料
				String oldpartNumber = job.getString("oldNum");
				// 变更前用量
				String oldamount = job.getString("oldAmount");
				// 行号
				String mainLineNumber = job.getString("mainLineNumber");
				// 变更后用量
				String newpartamount = job.getString("newAmount");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				//主料
				String mainNum = job.getString("mainNum");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.UpdateSubstitutesQuantity);
				bean.setSubstitutesNum(oldpartNumber);
				bean.setSubOldAmount(oldamount);
				bean.setMainLineNumber(mainLineNumber);
				bean.setSubNewAmount(newpartamount);
				bean.setEffectnumber(effectNumber);
				bean.setMainNum(mainNum);

				ECOCRUDUtil.createECO(bean);
			}
		} catch (JSONException e) {
			result = e.getMessage();
			log.error(e.getMessage(), e);
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 更新物料与BOM的关系属性
	 * 
	 * @param bean
	 * @param type
	 */

	public static String updateBom(BOMBean bean, String type) {
		String result = "";
		// 先取关系
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======updateBom bompart========" + ver);
			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			// 检出受影响对象
			bompart = (WTPart) GenericUtil.checkout(bompart);
			QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
			Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
			Map<String, WTPartUsageLink> linenumberlink = new HashMap<String, WTPartUsageLink>();
			while (groupqrq.hasMoreElements()) {
				WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
				String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
				String linenumber = puse.getLineNumber().toString();
				linklist.put(number, puse);
				linenumberlink.put(linenumber, puse);
			}
			String amout1 = bean.getNewpartamount();
			// 若存在对应关系则更新用量
			if (linklist.containsKey(bean.getOldpartnumber())
					&& linenumberlink.containsKey(bean.getOldpartlinenumber())) {
				if (Commit.equals(type)) {
					WTPartUsageLink link = linenumberlink.get(bean.getOldpartlinenumber());
					if (!BOMHelp.isNullOrEmpty(amout1)) {
						Quantity qu = link.getQuantity();
						qu.setAmount(Double.parseDouble(amout1));
						link.setQuantity(qu);
					}
					System.out.println("==========更新用量========");
					PersistenceHelper.manager.modify(link);
					// 检出受影响对象
					GenericUtil.checkin(bompart);
				} /*
					 * else if (Submit.equals(type)) {
					 * ECOCRUDUtil.createECO(bean); }
					 */

			}
		} catch (Exception e) {
			result += e.getMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}
	
	/**
	 * 修改替代料用量（流程结束时）
	 * @param bean
	 * @param type
	 * @return
	 * @author xucr
	 * @CreateDate 2021年1月20日
	 */
	public static String UpdateSubstitutesQuantity(BOMBean bean) {
		String result = "";
		// 先取关系
		try {
			WTPart bompart = CoreUtil.getWTPartByNumberAndView(bean.getEffectnumber(), bean.getView());
			WTPart mainPart = CoreUtil.getWTPartByNumberAndView(bean.getMainNum(), bean.getView());//主料
			WTPart stituePart = CoreUtil.getWTPartByNumberAndView(bean.getSubstitutesNum(), bean.getView());//替代料
			String newAmount = bean.getSubNewAmount();//新用量
			//找到使用关系
			QueryResult qr = PersistenceHelper.manager.find(
					WTPartUsageLink.class,bompart,"usedBy", (WTPartMaster)(mainPart.getMasterReference().getObject()));//使用关系
			//找到替代关系：只会找到一条
			if(qr.size() > 0 && !BOMHelp.isNullOrEmpty(newAmount)) {
				WTPartUsageLink link2 = (WTPartUsageLink) qr.nextElement();
				WTCollection wtcol2 = WTPartHelper.service.getSubstituteLinks(link2);
				WTPartSubstituteLink sublink2 = SubstituteLinkUtil.getSubstituteLink(
						(WTPartMaster) stituePart.getMasterReference().getObject(), wtcol2);
				
				// 检出受影响对象
				// 不需要检出物料状态
//				bompart = (WTPart) GenericUtil.checkout(bompart);
//				//更新替代料用量
//				SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
//				subQuantity1.setAmount(Double.parseDouble(newAmount));
//				sublink2.setQuantity(subQuantity1);
////				PersistenceHelper.manager.save(sublink2);
//				GenericUtil.checkin(bompart);
				
				//更新替代料用量
				SubstituteQuantity subQuantity1 = sublink2.getQuantity();
				subQuantity1.setAmount(Double.parseDouble(newAmount));
				sublink2.setQuantity(subQuantity1);
				PersistenceHelper.manager.modify(sublink2);
				//取消检出物料状态,之修改替代料用量
//				GenericUtil.checkin(bompart);
			}
		} catch (Exception e) {
			result += e.getLocalizedMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 新增替代料
	 */
	public static String addReplacePartBOM(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 主料号
				String partANumber = job.getString("numberA");
				// 主料行号
				String partAlinenumber = job.getString("linenumberA");
				// 替代料号
				String partBNumber = job.getString("numberB");
				// 替代料用量
				String AmountB = job.getString("amountB");
				// 替代料行号
				// String linenumberB = job.getString("linenumberB");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.ADDReplcePart);
				bean.setPartAnumber(partANumber);
				bean.setPartAlinenumber(partAlinenumber);
				bean.setPartBnumber(partBNumber);
				bean.setPartBamount(AmountB);
				// bean.setPartBlinenumber(linenumberB);
				bean.setEffectnumber(effectNumber);
				// 新增 替代料
				// addReplacePartToBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);

			}
		} catch (JSONException e) {
			log.error(e.getMessage(), e);
			result += e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 创建主料A与替换料B的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 */
	public static String addReplacePartToBom(BOMBean bean, String type) {
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());

			WTPart partA = PartUtil.getWTPartByNumber(bean.getPartAnumber());
			partA = GenericUtil.getPart(bean.getPartAnumber(), "", bean.getView());
			
			WTPart partB = PartUtil.getWTPartByNumber(bean.getPartBnumber());
			partB = GenericUtil.getPart(bean.getPartBnumber(), "", bean.getView());
			
			if (bompart != null && partA != null && partB != null) {

				String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
				System.out.println("=======addReplacePartToBom bompart ver========" + ver);
				String verA = VersionControlHelper.getIterationDisplayIdentifier(partA).toString();
				System.out.println("=======addReplacePartToBom partA ver========" + verA);
				String verB = VersionControlHelper.getIterationDisplayIdentifier(partB).toString();
				System.out.println("=======addReplacePartToBom partB ver========" + verB);

				WTPartMaster childAmaster = MyPartUtil.getWTPartMasterByNumber(bean.getPartAnumber());
				childAmaster = (WTPartMaster)partA.getMaster();
				
				WTPartMaster childBmaster = MyPartUtil.getWTPartMasterByNumber(bean.getPartBnumber());
				childBmaster = (WTPartMaster)partB.getMaster();
				
				bompart = (WTPart) GenericUtil.checkout(bompart);

				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				List<String> partlist = new ArrayList<String>();
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				Map<String, WTPartUsageLink> linklist2 = new HashMap<String, WTPartUsageLink>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					HashMap hm = new HashMap<String, String>();
					// A 父节点 B 子节点 ((WTPartMaster)
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber().toString();
					partlist.add(number);
					linklist.put(number, puse);
					linklist2.put(linenumber, puse);
				}

				// 受影响对象BOM下包含主料时 创建主料与替换料的关系
				if (partlist.contains(bean.getPartAnumber())) {
					if (Commit.equals(type)) {
						System.out.println("==========创建主料与替换料的关系   start==========");
						WTPartUsageLink newuserlink = linklist.get(bean.getPartAnumber());
						if(linklist2.containsKey(bean.getPartAlinenumber())){
							newuserlink = linklist2.get(bean.getPartAlinenumber());
						}
						
						QuantityUnit qu = null;
						if (childBmaster != null) {
							qu = childBmaster.getDefaultUnit();
						}
						// String quantity = "";
						SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
						if (!BOMHelp.isNullOrEmpty(bean.getPartBamount())) {
							subQuantity1.setAmount(Double.parseDouble(bean.getPartBamount()));
						}
						subQuantity1.setUnit(qu);

						SubstituteLinkUtil.createSubstituteLink(subQuantity1, newuserlink, childBmaster);

						GenericUtil.checkin(bompart);
						System.out.println("=========newuserlink=======" + newuserlink);
						System.out.println("==========创建主料与替换料的关系   end==========");
					} else if (Submit.equals(type)) {
						ECOCRUDUtil.createECO(bean);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("==========addReplacePartToBom   Exception:===" + e);
			result += e.getMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 删除替换料
	 */
	public static String deleteReplacePartBOM(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 主料号
				String partANumber = job.getString("numberA");
				// 主料行号
				String partAlinenumber = job.getString("linenumberA");
				// 替代料号
				String partBNumber = job.getString("numberB");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.DelReplcePart);
				bean.setPartAnumber(partANumber);
				bean.setPartAlinenumber(partAlinenumber);
				bean.setPartBnumber(partBNumber);
				bean.setEffectnumber(effectNumber);
				// 校验
				// 删除替代料
				// deleteReplacePartToBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);

			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result = e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 删除主料与替换料的关系
	 * 
	 * @param bean
	 * 
	 */
	public static String deleteReplacePartToBom(BOMBean bean, String type) {
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======deleteReplacePartToBom bompart========" + ver);
			WTPart partA = PartUtil.getWTPartByNumber(bean.getPartAnumber());

			WTPart partB = PartUtil.getWTPartByNumber(bean.getPartBnumber());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getPartBnumber());
			if (bompart != null && partA != null && partB != null) {
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				
				Map<String, WTPartUsageLink> linklist2 = new HashMap<String, WTPartUsageLink>();
				// Map<String, WTPartUsageLink> childlinklist = new
				// HashMap<String, WTPartUsageLink>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber() == null ? "" : puse.getLineNumber().toString();
					linklist.put(number, puse);
					linklist2.put(linenumber, puse);
				}
				// 确认受影响对象BOM结构下包含主料A
				if (linklist.containsKey(bean.getPartAnumber())) {
					// 检出主料
					// partA = (WTPart) GenericUtil.checkout(partA);
					WTPartUsageLink puse = linklist.get(bean.getPartAnumber());
					if(linklist2.containsKey(bean.getPartAlinenumber())){
						puse = linklist2.get(bean.getPartAlinenumber());
					}
					WTCollection wtcol = WTPartHelper.service.getSubstituteLinks(puse);

					WTPartSubstituteLink sublink = SubstituteLinkUtil.getSubstituteLink(childmaster, wtcol);
					System.out.println("=========删除替代料关系 1 ==========" + sublink);
					if (sublink != null) {
						PersistenceHelper.manager.delete(sublink);
					}
					GenericUtil.checkin(bompart);
					System.out.println("=========删除替代料关系  end ==========" + sublink);
				}
			}
		} catch (Exception e) {
			result += e.getMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 更换替代料
	 */
	public static String replaceOldPartBom(JSONArray array) {
		JSONObject object = new JSONObject();
		String result = "";
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 主料料号
				String partANumber = job.getString("partAnumber");
				// 行号
				String partAlinenumber = job.getString("partAlinenumber");
				// 原替换料料号
				String oldpartNumber = job.getString("oldpartnumber");
				// 原替换料用量
				String oldpartamount = job.getString("oldpartamount");
				// 新替换料料号
				String newpartNumber = job.getString("newpartnumber");
				// 新替换料用量
				String newpartAmount = job.getString("newpartamount");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.UpdateReplcePart);
				bean.setPartAnumber(partANumber);
				bean.setPartAlinenumber(partAlinenumber);
				bean.setOldpartnumber(oldpartNumber);
				bean.setOldpartamount(oldpartamount);
				bean.setNewpartnumber(newpartNumber);
				bean.setNewpartamount(newpartAmount);

				// bean.setNewpartlinenumber(newpartlinenumber);
				bean.setEffectnumber(effectNumber);
				// 校验

				// 先删除原替代料
				// deleteOldReplacePartBToBom(bean,Submit);
				// 更换新替代料
				// addNewReplacePartBToBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result = e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 创建主料A与新的替换料B的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 */
	public static void addNewReplacePartBToBom(BOMBean bean, String type) {

		// 取关系先判断是否已经存在关系
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			System.out.println("=======addNewReplacePartBToBom   newnumber===============" + bean.getNewpartnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			// 主料
			WTPart partA = PartUtil.getWTPartByNumber(bean.getPartAnumber());
			// 新替代料
			WTPart newReplacePart = PartUtil.getWTPartByNumber(bean.getNewpartnumber());

			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());

			bompart = (WTPart) GenericUtil.checkout(bompart);
			QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
			Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
			while (groupqrq.hasMoreElements()) {
				WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
				String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
				linklist.put(number, puse);
			}

			// 受影响对象BOM下包含主料A时 创建主料与新的替换料的关系
			if (linklist.containsKey(bean.getPartAnumber())) {
				if (Commit.equals(type)) {
					System.out.println("========创建主料与新替代料的关系===========");

					WTPartUsageLink newuserlink = linklist.get(bean.getPartAnumber());
					System.out.println("========创建主料与新替代料的关系 11111111===========" + childmaster);
					QuantityUnit qu = null;
					if (childmaster != null) {
						qu = childmaster.getDefaultUnit();
					}
					// String quantity = "";
					SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
					if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
						subQuantity1.setAmount(Double.parseDouble(bean.getNewpartamount()));
					}
					subQuantity1.setUnit(qu);

					// double sbustitueQuantity = Double.parseDouble( bean
					// .getNewpartamount());
					System.out.println("========创建主料与新替代料的关系 2222222222===========" + subQuantity1);
					// SubstituteQuantity subQuantity =
					// SubstituteQuantity.newSubstituteQuantity(sbustitueQuantity,
					// qu);

					SubstituteLinkUtil.createSubstituteLink(subQuantity1, newuserlink, childmaster);

					GenericUtil.checkin(bompart);
					System.out.println("========创建主料与新替代料的关系 333===========");
					// 检出主料
					/*
					 * partA = (WTPart) GenericUtil.checkout(partA);
					 * WTPartUsageLink newuserlink = WTPartUsageLink
					 * .newWTPartUsageLink(partA, childmaster); if
					 * (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
					 * Quantity qu = Quantity.newQuantity();
					 * qu.setAmount(Double.parseDouble(bean.getNewpartamount()))
					 * ; newuserlink.setQuantity(qu); } String linenumber =
					 * bean.getNewpartlinenumber(); int linenum =
					 * BOMHelp.getMaxLineNumber(partA); if
					 * (!BOMHelp.isNullOrEmpty(linenumber)) {
					 * newuserlink.setLineNumber(LineNumber
					 * .newLineNumber(Long.parseLong(linenumber))); }else{
					 * newuserlink.setLineNumber(LineNumber
					 * .newLineNumber(linenum)); }
					 * PersistenceHelper.manager.save(newuserlink);
					 * GenericUtil.checkin(partA);
					 */
					addNewPartToBom(bean, BOMECOUpdate.Commit);
				} else if (Submit.equals(type)) {
					ECOCRUDUtil.createECO(bean);
				}
			}
		} catch (Exception e) {
			System.out.println("=========addNewReplacePartBToBom==========Exception:" + e);
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * 删除主料与原替换料的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 * @throws Exception
	 */
	public static String deleteOldReplacePartBToBom(BOMBean bean, String type) {
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			WTPart partA = PartUtil.getWTPartByNumber(bean.getPartAnumber());

			WTPart oldpart = PartUtil.getWTPartByNumber(bean.getOldpartnumber());

			WTPartMaster oldpartmaster = MyPartUtil.getWTPartMasterByNumber(bean.getOldpartnumber());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());
			bompart = (WTPart) GenericUtil.checkout(bompart);

			QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
			Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
			
			Map<String, WTPartUsageLink> linklist2 = new HashMap<String, WTPartUsageLink>();
			while (groupqrq.hasMoreElements()) {
				WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
				String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
				String linenumber = puse.getLineNumber() == null ? "" : puse.getLineNumber().toString();
				linklist.put(number, puse);
				linklist2.put(linenumber, puse);
			}

			// 确认受影响对象BOM结构下包含主料A
			if (linklist.containsKey(bean.getPartAnumber())) {

				WTPartUsageLink puse = linklist.get(bean.getPartAnumber());
				if(linklist2.containsKey(bean.getPartAlinenumber())){
					puse = linklist2.get(bean.getPartAlinenumber());
				}
				WTCollection wtcol = WTPartHelper.service.getSubstituteLinks(puse);

				WTPartSubstituteLink sublink = SubstituteLinkUtil.getSubstituteLink(oldpartmaster, wtcol);
				System.out.println("=========删除替代料关系 1 ==========" + sublink);
				if (sublink != null) {
					System.out.println("=========删除原替代料关系  ==========");
					PersistenceHelper.manager.delete(sublink);
				}
				QuantityUnit qu = null;
				System.out.println("=========getNewpartamount  ==========" + bean.getNewpartamount());
				if (childmaster != null) {
					qu = childmaster.getDefaultUnit();
				}
				// String quantity = "";
				SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
				if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
					subQuantity1.setAmount(Double.parseDouble(bean.getNewpartamount()));
				}
				subQuantity1.setUnit(qu);

				// double sbustitueQuantity = Double.parseDouble( bean
				// .getNewpartamount());
				System.out.println("========创建主料与新替代料的关系 2222222222===========" + subQuantity1);
				// SubstituteQuantity subQuantity =
				// SubstituteQuantity.newSubstituteQuantity(sbustitueQuantity,
				// qu);

				SubstituteLinkUtil.createSubstituteLink(subQuantity1, puse, childmaster);
				PersistenceHelper.manager.modify(puse);
				GenericUtil.checkin(bompart);
				// addNewReplacePartBToBom(bean,BOMECOUpdate.Commit);
				/*
				 * // 检出主料 partA = (WTPart) GenericUtil.checkout(partA);
				 * QueryResult groupqrqB = WTPartHelper.service
				 * .getUsesWTPartMasters(partA); Map<String, WTPartUsageLink>
				 * linklistB = new HashMap<String, WTPartUsageLink>(); while
				 * (groupqrqB.hasMoreElements()) { WTPartUsageLink puse =
				 * (WTPartUsageLink) groupqrqB .nextElement(); String number =
				 * ((WTPartMaster) puse.getRoleBObject()) .getNumber(); String
				 * linenumber = puse.getLineNumber() == null ? "" :
				 * puse.getLineNumber().toString(); linklistB.put(number, puse);
				 * } // 删除主料A与替换料B的关系 if
				 * (linklistB.containsKey(bean.getOldpartnumber())) { if
				 * (Commit.equals(type)) {
				 * PersistenceHelper.manager.delete(linklistB.get(bean
				 * .getOldpartnumber())); } else if (Submit.equals(type)) {
				 * ECOCRUDUtil.createECO(bean); }
				 * 
				 * } // 检入主料 GenericUtil.checkin(partA);
				 */
			}
		} catch (Exception e) {
			result += e.getMessage();
			System.out.println("==========deleteOldReplacePartBToBom=======Exception:" + e);
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 更换主料
	 * 
	 */
	public static String repalcePartA(JSONArray array) {
		JSONObject object = new JSONObject();
		String result = "";
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 原主料料号
				String oldpartNumber = job.getString("oldpartnumber");
				// 原主料用量
				String oldpartamount = job.getString("oldpartamount");
				// 原主料行号
				String oldpartlinenumber = job.getString("oldpartlinenumber");
				// 新主料料料号
				String newpartNumber = job.getString("newpartnumber");
				// 新主料料用量
				String newpartAmount = job.getString("newpartamount");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setType(BOMBean.UpdateAPart);
				bean.setOldpartnumber(oldpartNumber);
				bean.setOldpartamount(oldpartamount);
				bean.setOldpartlinenumber(oldpartlinenumber);
				bean.setNewpartnumber(newpartNumber);
				bean.setNewpartamount(newpartAmount);
				// bean.setNewpartlinenumber(newpartlinenumber);
				bean.setEffectnumber(effectNumber);

				// 先删除原主料与受影响对象的关系
				// deleteOldPartAndEffectRelation(bean,Submit);
				// deletePartBom(bean,Submit);
				// 更换新替代料
				// addNewPartToBom(bean,Submit);
				ECOCRUDUtil.createECO(bean);

			}
		} catch (JSONException e) {
			log.error(e.getMessage(), e);
			result = e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();
	}

	/**
	 * 创建新主料与BOM的关系
	 * 
	 * @param bean
	 * 
	 */
	public static void addNewPartToBom(BOMBean bean, String type) {
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======addNewPartToBom bompart========" + ver);

			WTPart oldpart = PartUtil.getWTPartByNumber(bean.getOldpartnumber());

			WTPart newpart = PartUtil.getWTPartByNumber(bean.getNewpartnumber());
			// 新主料
			WTPartMaster newchildmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());
			System.out.println("=======addNewPartToBom newchildmaster========" + newchildmaster);
			System.out.println("=======addNewPartToBom new number========" + bean.getNewpartnumber());

			// if (!linklist.contains(bean.getNewpartnumber())) {

			QueryResult bomqs = WTPartHelper.service.getUsesWTPartMasters(bompart);

			QueryResult qs = WTPartHelper.service.getUsesWTPartMasters(oldpart);
			// 原主料下关系集合
			List<BOMBean> partlist = new ArrayList<BOMBean>();
			while (qs.hasMoreElements()) {
				WTPartUsageLink puse = (WTPartUsageLink) qs.nextElement();
				BOMBean bombean = new BOMBean();
				String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
				String linenumber = puse.getLineNumber().toString();
				String amount = String.valueOf(puse.getQuantity().getAmount());
				bombean.setOldpartnumber(number);
				bombean.setOldpartlinenumber(linenumber);
				bombean.setOldpartamount(amount);
				partlist.add(bombean);
			}
			if (Commit.equals(type)) {
				// 创建受影响对象与新主料的关系
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				while (bomqs.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) bomqs.nextElement();
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					linklist.put(number, puse);
				}
				if (linklist.containsKey(bean.getOldpartnumber())) {
					WTPartUsageLink olduserlink = linklist.get(bean.getOldpartnumber());
				}
				System.out.println("=======check out bompart======");
				// 检出
				bompart = (WTPart) GenericUtil.checkout(bompart);
				WTPartUsageLink newuserlink = WTPartUsageLink.newWTPartUsageLink(bompart, newchildmaster);
				String amout1 = bean.getNewpartamount();
				System.out.println("=======11111111111======");
				Quantity qu = Quantity.newQuantity();
				qu.setUnit(newchildmaster.getDefaultUnit());
				if (!BOMHelp.isNullOrEmpty(amout1)) {
					qu.setAmount(Double.parseDouble(amout1));
					newuserlink.setQuantity(qu);
				}
				String linenumber = bean.getNewpartlinenumber();
				System.out.println("=======222222222======");
				int linenum = BOMHelp.getMaxLineNumber(bompart);
				if (!BOMHelp.isNullOrEmpty(linenumber)) {
					newuserlink.setLineNumber(LineNumber.newLineNumber(Long.parseLong(linenumber)));
					System.out.println("=======333333333======");
				} else {
					newuserlink.setLineNumber(LineNumber.newLineNumber(linenum));
					System.out.println("=======44444444======");
				}
				System.out.println("=======55555555555======");
				PersistenceHelper.manager.save(newuserlink);
				// 检入
				GenericUtil.checkin(bompart);
				System.out.println("=======check in bompart======");
				// }
				// 创建新主料与原主料下的关系
				// 检出
				newpart = (WTPart) GenericUtil.checkout(newpart);
				for (BOMBean beanobj : partlist) {
					System.out.println("=======oldpartnumber:===" + beanobj.getOldpartnumber());
					WTPartMaster childmasterB = MyPartUtil.getWTPartMasterByNumber(beanobj.getOldpartnumber());
					WTPartUsageLink link = WTPartUsageLink.newWTPartUsageLink(newpart, childmasterB);
					String amout2 = beanobj.getOldpartamount();
					if (!BOMHelp.isNullOrEmpty(amout2)) {
						Quantity qu2 = Quantity.newQuantity();
						qu2.setAmount(Double.parseDouble(amout2));
						// qu.setUnit(QuantityUnit.EA);
						link.setQuantity(qu2);
					}
					int linenum2 = BOMHelp.getMaxLineNumber(newpart);
					String linenumber2 = beanobj.getOldpartlinenumber();
					if (!BOMHelp.isNullOrEmpty(linenumber2)) {
						link.setLineNumber(LineNumber.newLineNumber(Long.parseLong(linenumber2)));
					} else {
						link.setLineNumber(LineNumber.newLineNumber(linenum2));
					}
					PersistenceHelper.manager.save(link);
					System.out.println("=======save link:===");
				}
				// 检入
				GenericUtil.checkin(newpart);
				System.out.println("=======check in new part======");
			} /*
				 * else if (Submit.equals(type)) { ECOCRUDUtil.createECO(bean);
				 * }
				 */

		} catch (Exception e) {
			System.out.println("======addNewPartToBom======Exception:" + e);
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * 主次替换
	 * 
	 */
	public static String repalcePartAAndPartB(JSONArray array) {
		String result = "";
		JSONObject object = new JSONObject();
		try {
			for (int i = 0; i < array.length(); i++) {
				JSONObject job = (JSONObject) array.get(i);
				BOMBean bean = new BOMBean();
				// 原主料料号
				String partANumber = job.getString("partANumber");
				// 原替代料号
				String partBNumber = job.getString("partBNumber");
				// 受影响对象编号
				String effectNumber = job.getString("effectnumber");
				// 行号
				String linenumber = "";
				if(job.has("linenumber")){
					linenumber = job.getString("linenumber");
				}
				
				// 视图
				String view = job.getString("view");
				// ECO oid
				String oid = job.getString("ecoid");
				//changetype   0 单个变更   1 批量变更
				String changetype = "";
				if(job.has("changetype")){
					changetype = job.getString("changetype");
				}
				bean.setChangetype(changetype);
				bean.setEcoid(oid);
				bean.setView(view);
				bean.setOldpartnumber(partANumber);
				bean.setOldpartlinenumber(linenumber);
				bean.setNewpartnumber(partBNumber);
				// bean.setNewpartlinenumber(newpartlinenumber);
				bean.setEffectnumber(effectNumber);
				bean.setType(BOMBean.ReplaceABPart);

				// 校验

				// 先删除原主料与受影响对象的关系
				// deletePartBom(bean,Submit);
				// 再删除原主料与替代料的关系
				// bean = new BOMBean();
				// bean.setOldpartnumber(partBNumber);
				// bean.setEffectnumber(partANumber);
				// deletePartBom(bean,Submit);
				// 创建BOM与原替代料，原替代料与主料的关系
				// bean = new BOMBean();
				// bean.setOldpartnumber(partANumber);
				// bean.setNewpartnumber(partBNumber);
				// bean.setEffectnumber(effectNumber);
				// addBomAAndBRelation(bean,Submit);
				// CEOCRUD
				ECOCRUDUtil.createECO(bean);

			}
		} catch (JSONException e) {
			log.error(e.getMessage(), e);
			result = e.getMessage();
		} finally {
			try {
				object.put("tmcode", BOMHelp.correctcode);
				object.put("result", result);
			} catch (JSONException e) {
				log.error(e.getMessage(), e);
			}
		}
		return object.toString();

	}

	/**
	 * 创建受影响对象与原替代料，原替代料与原主料的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 * @throws Exception
	 */
	public static void addBomAAndBRelation(BOMBean bean, String type) {
		try {
			WTPart bompart = PartUtil.getWTPartByNumber(bean.getEffectnumber());
			bompart = GenericUtil.getPart(bean.getEffectnumber(), "", bean.getView());
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======addBomAAndBRelation bompart========" + ver);
			WTPart oldpart = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			System.out.println("=======addBomAAndBRelation oldpart========" + oldpart);
			WTPart newpart = PartUtil.getWTPartByNumber(bean.getNewpartnumber());
			System.out.println("=======addBomAAndBRelation newpart========" + newpart);
			// 主料
			WTPartMaster newchildmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());

			// 替代料
			WTPartMaster oldchildmaster = MyPartUtil.getWTPartMasterByNumber(bean.getOldpartnumber());
			if (bompart != null && oldpart != null && newpart != null) {
				// 检出受影响对象
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult bomqs = WTPartHelper.service.getUsesWTPartMasters(bompart);

				// 创建受影响对象与新主料的关系
				/*
				 * List<String> linklist = new ArrayList<String>(); while
				 * (bomqs.hasMoreElements()) { WTPartUsageLink puse =
				 * (WTPartUsageLink) bomqs .nextElement(); String number =
				 * ((WTPartMaster) puse.getRoleBObject()) .getNumber();
				 * linklist.add(number); }
				 */
				if (Commit.equals(type)) {
					// if (!linklist.contains(bean.getNewpartnumber())) {
					WTPartUsageLink newuserlink = WTPartUsageLink.newWTPartUsageLink(bompart, newchildmaster);
					int linenum2 = BOMHelp.getMaxLineNumber(bompart);
					newuserlink.setLineNumber(LineNumber.newLineNumber(linenum2));
					PersistenceHelper.manager.save(newuserlink);
					// 检入
					GenericUtil.checkin(bompart);
					String ver2 = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
					System.out.println("=======addBomAAndBRelation bompart ver2========" + ver2);
					System.out.println("=======addBomAAndBRelation 创建受影响对象与原替代料的关系========");
					// }
					// 创建原替代料与原主料的关系
					// 检出原替代料
					newpart = (WTPart) GenericUtil.checkout(newpart);
					/*
					 * QueryResult qs = WTPartHelper.service
					 * .getUsesWTPartMasters(newpart); List<String> linklist2 =
					 * new ArrayList<String>(); while (qs.hasMoreElements()) {
					 * WTPartUsageLink puse = (WTPartUsageLink) qs
					 * .nextElement(); String number = ((WTPartMaster)
					 * puse.getRoleBObject()) .getNumber();
					 * linklist2.add(number); }
					 */
					// if (!linklist2.contains(bean.getOldpartnumber())) {
					WTPartUsageLink link = WTPartUsageLink.newWTPartUsageLink(newpart, oldchildmaster);
					linenum2 = BOMHelp.getMaxLineNumber(newpart);
					link.setLineNumber(LineNumber.newLineNumber(linenum2));
					PersistenceHelper.manager.save(link);
					// 检入
					GenericUtil.checkin(newpart);
					System.out.println("=======addBomAAndBRelation 创建原替代料与原主料的关系========");
					// }
				} else if (Submit.equals(type)) {
					ECOCRUDUtil.createECO(bean);
				}
			}

		} catch (Exception e) {
			System.out.println("=======addBomAAndBRelation bompart========" + e);
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * 删除物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 */
	public static Map<String, String> deletePartBom2(BOMBean bean, String type) {
		Map<String, String> partlist = new HashMap<String,String>();
		
		System.out.println("=======deletePartBom start========");
		// 先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			System.out.println("=======deletePartBom bompart number========" + bomnumber + "===view:" + bean.getView());
			if (!BOMHelp.isNullOrEmpty(bean.getView())) {
				bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			}
			System.out.println("=======deletePartBom bompart========" + bompart);
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======deletePartBom bompart========" + ver);
			/*
			 * for (WTPart part1 : list) { if
			 * (part1.getViewName().equals(bean.getView())) { bompart = part1; }
			 * }
			 */

			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			if (bompart != null && part != null) {
				// 检出
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				Map<String, WTPartUsageLink> linenumberlist = new HashMap<String, WTPartUsageLink>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber() == null ? "" : puse.getLineNumber().toString();
					linklist.put(number, puse);
					System.out.println("========deletePartBom link linenumber==========:" + linenumber);
					linenumberlist.put(linenumber, puse);
				}
				System.out.println("========linklist.size()==========" + linklist.size());
				// 找出并删掉关系
				if (linklist.containsKey(bean.getOldpartnumber())) {
					if (Commit.equals(type)) {
						System.out
								.println("========deletePartBom oldlinenumber==========" + bean.getOldpartlinenumber());
						System.out.println(
								"========deletePartBom==========" + linenumberlist.get(bean.getOldpartlinenumber()));
						if (bean.getOldpartlinenumber() != null
								&& linenumberlist.containsKey(bean.getOldpartlinenumber())) {
							WTPartUsageLink link = linenumberlist.get(bean.getOldpartlinenumber());
							
							partlist = SubstituteLinkUtil.getSubstituteLinkList(link);
							partlist.put(bean.getOldpartnumber(), bean.getOldpartamount());
							
							System.out.println("========deletePartBom by  linenumberlist==========");
							PersistenceHelper.manager.delete(link);
						} else {
							System.out.println("========deletePartBom by linklist==========");
							WTPartUsageLink link = linklist.get(bean.getOldpartnumber());
							
							partlist = SubstituteLinkUtil.getSubstituteLinkList(link);
							partlist.put(bean.getOldpartnumber(), bean.getOldpartamount());
							
							PersistenceHelper.manager.delete(link);
						}
					}
					/*
					 * if (Submit.equals(type)) { ECOCRUDUtil.createECO(bean); }
					 * else if (Commit.equals(type)) { System.out.println(
					 * "========delete link==========");
					 * PersistenceHelper.manager.delete(linklist.get(bean
					 * .getOldpartnumber())); }
					 */
				}
				// 检入
				if (WorkInProgressHelper.isCheckedOut(bompart)) {
					GenericUtil.checkin(bompart);
				}

				// addNewPartToBom(bean, BOMECOUpdate.Commit);
				System.out.println("========deletePartBom  end==========");
			}
		} catch (Exception e) {
			System.out.println("================删除关系失败===========e:" + e);
			log.error(e.getMessage(), e);
		}
		return partlist;

	}

	/**
	 * 删除物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 */
	public static Map deletePartBom3(BOMBean bean, String type,Map<String, String> partlist) {
		Map map = new HashMap();
		String errorlog = "";
		System.out.println("=======deletePartBom3 start========");
		// 先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			System.out.println("=======deletePartBom bompart number========" + bomnumber + "===view:" + bean.getView());
			if (!BOMHelp.isNullOrEmpty(bean.getView())) {
				bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			}
			System.out.println("=======deletePartBom bompart========" + bompart);
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======deletePartBom bompart========" + ver);
			/*
			 * for (WTPart part1 : list) { if
			 * (part1.getViewName().equals(bean.getView())) { bompart = part1; }
			 * }
			 */

			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());
			if (bompart != null && part != null) {
				// 检出
				bompart = (WTPart) GenericUtil.checkout(bompart);
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				Map<String, WTPartUsageLink> linklist = new HashMap<String, WTPartUsageLink>();
				Map<String, WTPartUsageLink> linenumberlist = new HashMap<String, WTPartUsageLink>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					String linenumber = puse.getLineNumber() == null ? "" : puse.getLineNumber().toString();
					linklist.put(number, puse);
					System.out.println("========deletePartBom link linenumber==========:" + linenumber);
					linenumberlist.put(linenumber, puse);
				}
				System.out.println("========linklist.size()==========" + linklist.size());
				// 找出并删掉关系
				if (linklist.containsKey(bean.getOldpartnumber())) {
					WTPartUsageLink puse = null;
					if (Commit.equals(type)) {
						System.out
								.println("========deletePartBom oldlinenumber==========" + bean.getOldpartlinenumber());
						System.out.println(
								"========deletePartBom==========" + linenumberlist.get(bean.getOldpartlinenumber()));
						if (bean.getOldpartlinenumber() != null
								&& linenumberlist.containsKey(bean.getOldpartlinenumber())) {
							puse = linenumberlist.get(bean.getOldpartlinenumber());
							System.out.println("========deletePartBom by  linenumberlist==========");
							WTCollection wtcol = WTPartHelper.service.getSubstituteLinks(puse);

							WTPartSubstituteLink sublink = SubstituteLinkUtil.getSubstituteLink(childmaster, wtcol);
								
							partlist = SubstituteLinkUtil.getSubstituteLinkList(puse);
							
							bean.setOldpartamount(String.valueOf(puse.getQuantity().getAmount()));
							if (sublink != null && sublink.getQuantity().getAmount() != null) {
								System.out.println("===========sublink amout 1:" + sublink.getQuantity().getAmount());
								bean.setNewpartamount(String.valueOf(sublink.getQuantity().getAmount()));
							}
							PersistenceHelper.manager.delete(puse);
						} else {
							puse = linklist.get(bean.getOldpartnumber());
							System.out.println("========deletePartBom by linklist==========");
							WTCollection wtcol = WTPartHelper.service.getSubstituteLinks(puse);
							WTPartSubstituteLink sublink = SubstituteLinkUtil.getSubstituteLink(childmaster, wtcol);
							
							partlist = SubstituteLinkUtil.getSubstituteLinkList(puse);
							
							bean.setOldpartamount(String.valueOf(puse.getQuantity().getAmount()));
							if (sublink != null && sublink.getQuantity().getAmount() != null) {
								System.out.println("===========sublink amout 2:" + sublink.getQuantity().getAmount());
								bean.setNewpartamount(String.valueOf(sublink.getQuantity().getAmount()));
							}
							PersistenceHelper.manager.delete(puse);

						}
					}
					/*
					 * if (Submit.equals(type)) { ECOCRUDUtil.createECO(bean); }
					 * else if (Commit.equals(type)) { System.out.println(
					 * "========delete link==========");
					 * PersistenceHelper.manager.delete(linklist.get(bean
					 * .getOldpartnumber())); }
					 */
				}
				// 检入
				if (WorkInProgressHelper.isCheckedOut(bompart)) {
					GenericUtil.checkin(bompart);
				}

				// addNewPartToBom(bean, BOMECOUpdate.Commit);
				System.out.println("========deletePartBom  end==========");
			}
		} catch (Exception e) {
			System.out.println("================删除关系失败===========e:" + e);
			errorlog += e.getMessage();
			log.error(e.getMessage(), e);
		}
		map.put("bean", bean);
		map.put("sublist", partlist);
		map.put("errorlog", errorlog);
		return map;
	}

	/**
	 * 创建物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 * @throws Exception
	 */
	public static String addPartToBom2(BOMBean bean, String type,Map<String,String> numberlist) {
		System.out.println("=======addPartToBom========" + type);
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======addPartToBom bompart========" + ver);
			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getOldpartnumber());
			if (bompart != null && part != null) {
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				List<String> partlist = new ArrayList<String>();
				//存放行号  避免填写的行号已存在于关系中
				List<String> linenumberlist = new ArrayList<String>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					String linenumber = puse.getLineNumber().toString();
					// A 父节点 B 子节点 ((WTPartMaster)
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					linenumberlist.add(linenumber);
					partlist.add(number);
				}
				System.out.println("=======addPartToBom partlist========" + partlist);
				// 检出受影响对象
				bompart = (WTPart) GenericUtil.checkout(bompart);

				// 创建
				// if (!partlist.contains(bean.getOldpartnumber())) {
				WTPartUsageLink newuserlink = WTPartUsageLink.newWTPartUsageLink(bompart, childmaster);
				String amout1 = bean.getOldpartamount();
				Quantity qu = Quantity.newQuantity();
				qu.setUnit(part.getDefaultUnit());
				if (!BOMHelp.isNullOrEmpty(amout1)) {
					System.out.println("=======number:" + part.getNumber() + "=====UNIT======" + part.getDefaultUnit());
					qu.setAmount(Double.parseDouble(amout1));
				}
				// qu.setUnit(QuantityUnit.EA);
				newuserlink.setQuantity(qu);
				int linenum = BOMHelp.getMaxLineNumber(bompart);
				// 行号为空时自动设置行号
				try {
					if (BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
						newuserlink.setLineNumber(LineNumber.newLineNumber(linenum + 10));
					} else {
						if(linenumberlist.contains(bean.getOldpartlinenumber())){
							newuserlink.setLineNumber(LineNumber.newLineNumber(linenum + 10));

						}else{
							newuserlink.setLineNumber(LineNumber.newLineNumber(Integer.parseInt(bean.getOldpartlinenumber())));

						}
					}

				} catch (WTPropertyVetoException e) {
					result += e.getMessage();
					System.out.println("=======addPartToBom WTPropertyVetoException========" + e);
					log.error(e.getMessage(), e);
				}
				if (Commit.equals(type)) {
					System.out.println("=======addPartToBom Commit========");
					/*
					 * if(BOMBean.ReplaceABPart.equals(bean.getType())){ WTPart
					 * newpart =
					 * PartUtil.getWTPartByNumber(bean.getNewpartnumber());
					 * WTPartMaster newchildmaster =
					 * MyPartUtil.getWTPartMasterByNumber(bean
					 * .getNewpartnumber()); QuantityUnit subqu = null ; if(
					 * newchildmaster != null ){ subqu =
					 * newchildmaster.getDefaultUnit() ; }
					 * 
					 * SubstituteQuantity subQuantity1 =
					 * SubstituteQuantity.newSubstituteQuantity();
					 * if(!BOMHelp.isNullOrEmpty(bean.getOldpartamount())){
					 * subQuantity1.setAmount(Double.parseDouble(bean.
					 * getOldpartamount())); } subQuantity1.setUnit(subqu);
					 * 
					 * SubstituteLinkUtil.createSubstituteLink(subQuantity1,
					 * newuserlink, newchildmaster);
					 * 
					 * System.out.println(
					 * "============ReplaceABPart 主次替换 创建主料与替换料的替换关系======"); }
					 */

					PersistenceHelper.manager.save(newuserlink);
					Iterator iter = numberlist.keySet().iterator();
					while(iter.hasNext()){
						String number = iter.next().toString();
						String amount = numberlist.get(number);
						System.out.println("========oldchildmaster number:"+number+"===amount:"+amount);
						WTPartMaster oldchildmaster = MyPartUtil.getWTPartMasterByNumber(number);
						QuantityUnit subqu = null;
						if (oldchildmaster != null) {
							subqu = oldchildmaster.getDefaultUnit();
						}
						SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
						if (!StringUtil.isNullOrEmpty(amount)) {
							subQuantity1.setAmount(Double.parseDouble(amount));
						}
						subQuantity1.setUnit(subqu);
						SubstituteLinkUtil.createSubstituteLink(subQuantity1, newuserlink, oldchildmaster);
						System.out.println("========create createSubstituteLink end===========");
					}
					
				} else if (Submit.equals(type)) {
					ECOCRUDUtil.createECO(bean);
				}
				// 检入受影响对象
				GenericUtil.checkin(bompart);
			}

			System.out.println("=======addPartToBom checkin========");
			// }
		} catch (Exception e) {
			System.out.println("=======addPartToBom Exception========" + e);
			result += e.getMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 创建物料与BOM的关系
	 * 
	 * @param partNumber
	 * @param effectNumber
	 * @throws Exception
	 */
	public static String addPartToBom3(BOMBean bean, String type,Map<String, String> subpartlist) {
		System.out.println("=======addPartToBom========" + type);
		String result = "";
		// 取关系先判断是否已经存在关系
		try {
			String bomnumber = bean.getEffectnumber();
			List<WTPart> list = BOMHelp.getAllLatestVersionPart(bomnumber);
			WTPart bompart = null;
			bompart = GenericUtil.getPart(bomnumber, "", bean.getView());
			if (bompart == null) {
				bompart = PartUtil.getWTPartByNumber(bomnumber);
			}
			String ver = VersionControlHelper.getIterationDisplayIdentifier(bompart).toString();
			System.out.println("=======addPartToBom bompart========" + ver);
			WTPart part = PartUtil.getWTPartByNumber(bean.getOldpartnumber());
			WTPartMaster childmaster = MyPartUtil.getWTPartMasterByNumber(bean.getOldpartnumber());
			if (bompart != null && part != null) {
				QueryResult groupqrq = WTPartHelper.service.getUsesWTPartMasters(bompart);
				List<String> partlist = new ArrayList<String>();
				while (groupqrq.hasMoreElements()) {
					WTPartUsageLink puse = (WTPartUsageLink) groupqrq.nextElement();
					HashMap hm = new HashMap<String, String>();
					// A 父节点 B 子节点 ((WTPartMaster)
					String number = ((WTPartMaster) puse.getRoleBObject()).getNumber();
					partlist.add(number);
				}
				System.out.println("=======addPartToBom partlist========" + partlist);
				// 检出受影响对象
				bompart = (WTPart) GenericUtil.checkout(bompart);

				// 创建
				// if (!partlist.contains(bean.getOldpartnumber())) {
				WTPartUsageLink newuserlink = WTPartUsageLink.newWTPartUsageLink(bompart, childmaster);
				String amout1 = bean.getOldpartamount();
				Quantity qu = Quantity.newQuantity();
				qu.setUnit(part.getDefaultUnit());
				if (!BOMHelp.isNullOrEmpty(amout1)) {
					System.out.println("=======number:" + part.getNumber() + "=====UNIT======" + part.getDefaultUnit());
					qu.setAmount(Double.parseDouble(amout1));
				}
				// qu.setUnit(QuantityUnit.EA);
				newuserlink.setQuantity(qu);
				int linenum = BOMHelp.getMaxLineNumber(bompart);
				// 行号为空时自动设置行号
				try {
					if (BOMHelp.isNullOrEmpty(bean.getOldpartlinenumber())) {
						newuserlink.setLineNumber(LineNumber.newLineNumber(linenum + 10));
					} else {
						newuserlink.setLineNumber(LineNumber.newLineNumber(Integer.parseInt(bean.getOldpartlinenumber())));
					}

				} catch (WTPropertyVetoException e) {
					result += e.getMessage();
					System.out.println("=======addPartToBom WTPropertyVetoException========" + e);
					log.error(e.getMessage(), e);
				}
				if (Commit.equals(type)) {
					System.out.println("=======addPartToBom Commit========");
					PersistenceHelper.manager.save(newuserlink);
					System.out.println("=======start create  ReplaceABPartRelation========");
					if (BOMBean.ReplaceABPart.equals(bean.getType())) {
						WTPart newpart = PartUtil.getWTPartByNumber(bean.getNewpartnumber());
						WTPartMaster newchildmaster = MyPartUtil.getWTPartMasterByNumber(bean.getNewpartnumber());
						QuantityUnit subqu = null;
						if (newchildmaster != null) {
							subqu = newchildmaster.getDefaultUnit();
						}
						SubstituteQuantity subQuantity1 = SubstituteQuantity.newSubstituteQuantity();
						if (!BOMHelp.isNullOrEmpty(bean.getNewpartamount())) {
							subQuantity1.setAmount(Double.parseDouble(bean.getNewpartamount()));
						}
						subQuantity1.setUnit(subqu);
						System.out.println("=======middle create  ReplaceABPartRelation========");
						SubstituteLinkUtil.createSubstituteLink(subQuantity1, newuserlink, newchildmaster);

						Iterator iter = subpartlist.keySet().iterator();
						System.out.println("=================subpartlist:"+subpartlist.size());
						while(iter.hasNext()){
							String number = iter.next().toString();
							System.out.println("==========childmaster.getNumber()======"+childmaster.getNumber());
							System.out.println("=================subpartlist:"+number);
							String amount = subpartlist.get(number);
							if(!number.equals(childmaster.getNumber())){
								System.out.println("========oldchildmaster number:"+number+"===amount:"+amount);
								WTPartMaster oldchildmaster = MyPartUtil.getWTPartMasterByNumber(number);
								QuantityUnit childsubqu = null;
								if (oldchildmaster != null) {
									childsubqu = oldchildmaster.getDefaultUnit();
								}
								SubstituteQuantity childsubQuantity1 = SubstituteQuantity.newSubstituteQuantity();
								if (!StringUtil.isNullOrEmpty(amount)) {
									childsubQuantity1.setAmount(Double.parseDouble(amount));
								}
								childsubQuantity1.setUnit(childsubqu);
								SubstituteLinkUtil.createSubstituteLink(childsubQuantity1, newuserlink, oldchildmaster);
								System.out.println("========create createSubstituteLink end===========");
							}
							
						}
						
						
						System.out.println("============ReplaceABPart 主次替换 创建主料与替换料的替换关系======");
					}
				} else if (Submit.equals(type)) {
					ECOCRUDUtil.createECO(bean);
				}
				// 检入受影响对象
				GenericUtil.checkin(bompart);
			}

			System.out.println("=======addPartToBom checkin========");
			// }
		} catch (Exception e) {
			System.out.println("=======addPartToBom Exception========" + e);
			result += e.getMessage();
			log.error(e.getMessage(), e);
		}
		return result;
	}

}
